var express = require('express')
var router = express.Router()
var fs = require('fs')

// 导入db ,操作数据库
const { getAll, getById, exec } = require('../db/index')

/**
 * 获取所有用户
 * GET /blogArticle
 */
router.get('/', async (req, res) => {
  // console.log(req);
  console.log(req.query)
  // 解析请求参数
  // 解构query对象,提取page,size
  // 如果没有值,就给默认值1,2
  const { page = 1, size = 4 } = req.query
  // 获取总数
  let sql = `select count(*) as total  from bk_articles`
  const obj = await getById(sql) // { total: 10 }
  console.log(obj)

  // 根据分页公式, 计算offset偏移量
  let offset = (page - 1) * size
  // 操作数据库
  sql = `select * from bk_articles order by article_id limit ${offset}, ${size}`
  const data = await getAll(sql)

  res.send({
    code: 0,
    message: '获取用户成功',
    result: {
      total: obj.total,
      data: data,
    },
  })
})
// 按文章标题查询文章
router.post('/articlename', async (req, res) => {
  const { page = 1, size = 4, article_title } = req.query
  // 获取总数
  let sql = `select count(*) as total  from bk_articles  where article_title like'%${article_title}%'`
  const obj = await getById(sql)
  console.log(obj)
  let offset = (page - 1) * size
  // 操作数据库
  sql = `select * from bk_articles where article_title like'%${article_title}%' order by article_id asc  limit ${offset}, ${size} `
  try {
    const data = await getAll(sql)
    res.send({
      code: 0,
      message: '获取用户成功',
      result: {
        total: obj.total,
        data: data,
      },
    })
  } catch (err) {
    res.send({
      code: 'err',
      message: '获取所有用户失败',
      result: debug ? '' : err,
    })
  }
})
/**
 * 新增
 */
router.post('/', async function (req, res) {
  let {
    article_title,
    article_content,
    article_date,
    article_picture,
    category_id,
  } = req.body
  let sql = `insert into bk_articles (article_title , article_content , article_picture,category_id) 
values  ('${article_title}', '${article_content}','${article_picture}','${category_id}')`
  console.log(sql)
  try {
    const data = await exec(sql)
    res.send({
      code: 0,
      message: '添加事件成功',
      result: '',
    })
  } catch (err) {
    res.send({
      code: 500,
      message: '添加事件失败',
      result: debug ? '' : err,
    })
  }
})

// 根据id获取文章
router.get('/:id', async (req, res) => {
  const { id } = req.params
  let sql = `select article_id,article_title,article_content,article_views,article_date,article_picture, count,category_id,article_like from bk_articles where article_id = ${id}`
  const data = await getById(sql)
  res.send({
    code: 0,
    message: '获取用户成功',
    result: {
      data,
    },
  })
})

// 修改请求
router.put('/:id', async (req, res) => {
  const { id } = req.params
  const { article_title, article_content, article_picture, category_id } =
    req.body
  let sql = `update bk_articles set article_title='${article_title}',
                                 article_content='${article_content}',
                                 article_picture='${article_picture}',
                                 category_id='${category_id}'
              where article_id=${id} `
  const data = await exec(sql)
  res.send({
    code: 0,
    message: '修改用户成功',
    result: {
      article_id: id,
      article_title,
      article_picture,
      article_content,
      category_id,
    },
  })
})
// 点赞接口
router.put('/:id/like', async (req, res) => {
  const { id } = req.params
  const { article_like } = req.body
  let sql = `update bk_articles set article_like='${article_like}'                  
              where article_id=${id} `

  const data = await exec(sql)
  res.send({
    code: 0,
    message: '修改点赞状态成功',
    result: {
      article_id: id,
      article_like,
    },
  })
})
// 删除
router.delete('/:id', async (req, res) => {
  const { id } = req.params
  let sql = `delete from bk_articles where article_id=${id}`
  await exec(sql)
  res.send({
    code: 0,
    message: '删除成功',
    result: '',
  })
})
module.exports = router
